#include "xtypes.h"
#include "util_vector.h"
#include "util_matrix.h"




vector3_t xVector3Sub(vector3_t L, vector3_t R)
{
	vector3_t t;
	t.x = L.x-R.x;
	t.y = L.y-R.y;
	t.z = L.z-R.z;
	return t;
}


vector3_t xVector3Add(vector3_t L, vector3_t R)
{
	vector3_t t;
	t.x = L.x+R.x;
	t.y = L.y+R.y;
	t.z = L.z+R.z;
	return t;
}

vector3_t xVector3Scale(vector3_t V, float_t s)
{
	vector3_t t;
	t.x = V.x * s;
	t.y = V.y * s;
	t.z = V.z * s;
	return t;
}


vector3_t xVector3Cross(vector3_t L, vector3_t R)
{
	vector3_t t;
	t.x = L.y * R.z - L.z * R.y;
	t.y = L.z * R.x - L.x * R.z;
	t.z = L.x * R.y - L.y * R.x;
	return t;
}



float_t xVector3Dot(vector3_t L, vector3_t R)
{
	return L.x * R.x + L.y * R.y + L.z * R.z;
}



float_t xVector3Modulus(vector3_t v)
{
	return xMath_sqrt(v.x*v.x + v.y*v.y+ v.z*v.z);
}

float_t xVector3ReciprocalModulus(vector3_t v)
{
	return xMath_rsqrt(v.x*v.x + v.y*v.y+ v.z*v.z);
}



vector3_t xVector3Normalize(vector3_t v)
{
	vector3_t t;
	float_t ilength;

	ilength = xMath_rsqrt(v.x*v.x + v.y*v.y+ v.z*v.z);

	t.x = v.x * ilength;
	t.y = v.y * ilength;
	t.z = v.z * ilength;

	return t;
}

vector3_t xVector3Inverse(vector3_t v)
{
	vector3_t t;

	t.x = -v.x;
	t.y = -v.y;
	t.z = -v.z;

	return t;
}


float_t xVector3Angle(vector3_t v1, vector3_t v2)
{
	return xACos(xVector3Dot(v1,v2) * xVector3ReciprocalModulus(v1) * xVector3ReciprocalModulus(v2));
}


vector3_t operator*( float L, vector3_t& R)
{
	return xVector3Scale(R, L);
}



vector3_t operator*(vector3_t& L, float R)
{
	return xVector3Scale(L, R);
}


vector3_t& operator+=(vector3_t& L, const vector3_t R)
{
	L = xVector3Add(L, R);
	return L;
}


vector3_t operator+(const vector3_t L, const vector3_t R)
{
	return xVector3Add(L, R);
}



vector3_t& operator-=(vector3_t& L, const vector3_t R)
{
	L = xVector3Sub(L, R);
	return L;
}


vector3_t operator-(const vector3_t L, const vector3_t R)
{
	return xVector3Sub(L, R);
}



vector3_t& operator*=(vector3_t& L, const vector3_t R)
{
	L = xVector3Cross(L, R);
	return L;
}


vector3_t operator*(const vector3_t L, const vector3_t R)
{
	return xVector3Cross(L, R);
}
